const { Model, DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    class ExchangeOrder extends Model {
        static associate(models) {
            // 定义关联关系
            ExchangeOrder.belongsTo(models.ExchangeGood, {
                foreignKey: 'goods_id',
                as: 'goods'
            });
            ExchangeOrder.belongsTo(models.User, {
                foreignKey: 'user_id',
                as: 'user'
            });
        }
    }

    ExchangeOrder.init(
        {
            id: {
                type: DataTypes.BIGINT,
                autoIncrement: true,
                primaryKey: true,
                comment: '订单ID'
            },
            user_id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                comment: '用户ID'
            },
            goods_id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                comment: '商品ID'
            },
            quantity: {
                type: DataTypes.INTEGER,
                allowNull: false,
                comment: '兑换数量'
            },
            points: {
                type: DataTypes.INTEGER,
                allowNull: false,
                comment: '消耗积分'
            },
            status: {
                type: DataTypes.TINYINT,
                defaultValue: 0,
                comment: '状态：0-待发货，1-已发货，2-已完成'
            },
            tracking_number: {
                type: DataTypes.STRING(50),
                comment: '物流单号'
            }
        },
        {
            sequelize,
            modelName: 'ExchangeOrder',
            tableName: 'exchange_orders',
            timestamps: true,
            paranoid: true,
            underscored: true
        }
    );

    return ExchangeOrder;
}; 